/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;

namespace DslEditorPowerToy.Controls.VirtualTreeGrid.Validation
{
	/// <summary>
	/// Represents a class used to validate arguments
	/// </summary>
	internal static class ArgumentValidator
	{
		#region Public Methods
		/// <summary>
		/// Validates given argument and if false raises exception
		/// </summary>
		/// <param name="argumentName">Name of argument validated</param>
		/// <param name="success">success of argument validation</param>
		static public void Validate(string argumentName, bool success)
		{
			Validate(argumentName, "", success);
		}

		/// <summary>
		/// Validates given argument and if false raises exception
		/// </summary>
		/// <param name="argumentName">Name of argument validated</param>
		/// <param name="Message">Message to display if success id false</param>
		/// <param name="success">success of argument validation</param>
		/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the value of <paramref name="success"/> is false</exception>
		static public void Validate(string argumentName, string message, bool success)
		{
			// Check success
			if (false == success)
			{
				//Raise Argument Exception
				throw new ArgumentOutOfRangeException(argumentName, message);
			}
		}

		/// <summary>
		/// Validates given argument is not null
		/// </summary>
		/// <param name="argumentName">Name of argument validated</param>
		/// <param name="argument">Argument to check for null</param>
		/// <exception cref="System.ArgumentNullException">Thrown when <paramref name="argument"/> is null</exception>
		static public void ValidateNotNull(string argumentName, object argument)
		{
			//Check if null
			if (null == argument)
			{
				//Raise Argument Exception
				throw new ArgumentNullException(argumentName);
			}
		}

		/// <summary>
		/// Validate given argument is of the specified type
		/// </summary>
		/// <param name="argumentName">Name of argument validated</param>
		/// <param name="argument">Argument to check for type</param>
		/// <param name="type">Type to check of argument</param>
		/// <exception cref="System.ArgumentOutOfRangeException">Thrown when <paramref name="argument"/> is not of the specified <paramref name="type"/></exception>
		static public void  ValidateIsTypeOf(string argumentName, object argument, Type type)
		{
			ValidateNotNull(argumentName, argument);
			ValidateNotNull("type", type);

			//Check type assignment
			if (!type.IsAssignableFrom(argument.GetType()))
				throw new ArgumentOutOfRangeException(argumentName);
		}

		/// <summary>
		/// Validates given argument is a valid enum
		/// </summary>
		/// <param name="argumentName">Name of argument validated</param>
		/// <param name="enumType">Enumeration type</param>
		/// <param name="value">value of enumeration to check</param>
		/// <exception cref="System.ArgumentOutOfRangeException">Thrown when <paramref name="value"/> does not represent a valid value in the enumeration type.</exception>
		static public void ValidateEnum(string argumentName, Type enumType, object value)
		{
			if (!Enum.IsDefined(enumType, value)) 
				throw new ArgumentOutOfRangeException(argumentName);
		}
	
		#endregion
	}
}
